﻿/*
7-36 复数四则运算 (15 分)
本题要求编写程序，计算2个复数的和、差、积、商。

输入格式：
输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。

输出格式：
分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商，数字精确到小数点后1位。如果结果的实部或者虚部为0，则不输出。如果结果为0，则输出0.0。

输入样例1：
2 3.08 -2.04 5.06
输出样例1：
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
输入样例2：
1 1 -1 -1.01
输出样例2：
(1.0+1.0i) + (-1.0-1.0i) = 0.0
(1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
(1.0+1.0i) * (-1.0-1.0i) = -2.0i
(1.0+1.0i) / (-1.0-1.0i) = -1.0
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef double RealType;
typedef struct {
	RealType real;
	RealType imag;
} Complex, * PComplex;
static Complex complex_add(Complex a, Complex b);
static Complex complex_sub(Complex a, Complex b);
static Complex complex_product(Complex a, Complex b);
static Complex complex_divide(Complex a, Complex b);
static Complex complex_read();
static void complex_print0(Complex a, int brace);
static void complex_print(Complex a, int brace);

static Complex complex_read() {
	Complex a = {};
	scanf("%lf %lf", &a.real, &a.imag);
	return a;
}

const RealType EPS = 0.1;
static int is_zero(RealType a) {
	return -EPS < a && a < EPS;
}

static void complex_print0(Complex a, int brace) {
	printf("(");
	if (a.imag >= 0)
		printf("%.1f+%.1fi", a.real, a.imag);
	else
		printf("%.1f%.1fi", a.real, a.imag);
	printf(")");
}

static void complex_print(Complex a, int brace) {
	if (brace)
		printf("(");
	if (is_zero(a.real) && is_zero(a.imag))
		printf("0.0");
	else if (is_zero(a.real))
		printf("%.1fi", a.imag);
	else if (is_zero(a.imag))
		printf("%.1f", a.real);
	else if (a.imag > 0)
		printf("%.1f+%.1fi", a.real, a.imag);
	else
		printf("%.1f%.1fi", a.real, a.imag);
	if (brace)
		printf(")");
}

static Complex complex_add(Complex a, Complex b) {
	Complex c = { a.real + b.real, a.imag + b.imag };
	return c;
}

static Complex complex_sub(Complex a, Complex b) {
	Complex c = { a.real - b.real, a.imag - b.imag };
	return c;
}

static Complex complex_product(Complex a, Complex b) {
	Complex c = { a.real * b.real - a.imag * b.imag, a.imag * b.real + a.real * b.imag };
	return c;
}

static Complex complex_divide(Complex a, Complex b) {
	Complex d = { b.real,-b.imag };
	Complex c = complex_product(a, d);
	d = complex_product(d, b);
	c.real /= d.real;
	c.imag /= d.real;
	return c;
}


int main() {
	freopen("D:/Develop/GitRepos/MOOC/浙江大学/数据结构/201906/zju_C_Basic/data/7.36.txt", "r", stdin);
	int n=0;
	Complex a = complex_read();
	Complex b = complex_read();

	complex_print0(a, 1);
	printf(" + ");
	complex_print0(b, 1);
	printf(" = ");
	complex_print(complex_add(a, b), 0);
	printf("\n");

	complex_print0(a, 1);
	printf(" - ");
	complex_print0(b, 1);
	printf(" = ");
	complex_print(complex_sub(a, b), 0);
	printf("\n");

	complex_print0(a, 1);
	printf(" * ");
	complex_print0(b, 1);
	printf(" = ");
	complex_print(complex_product(a, b), 0);
	printf("\n");

	complex_print0(a, 1);
	printf(" / ");
	complex_print0(b, 1);
	printf(" = ");
	complex_print(complex_divide(a, b), 0);
	printf("\n");


	return 0;
}